package ian.android.lovewords.ui;

import java.io.InputStream;
import java.util.ArrayList;

import ian.android.lovewords.R;
import ian.android.lovewords.time.TimeManip;
import ian.android.lovewords.util.StoreProgress;
import ian.android.lovewords.util.WordPagerAdapter;
import ian.android.lovewords.words.WordBlock;
import ian.android.lovewords.words.WordList;
import ian.android.lovewords.words.WordSchedule;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ReciteActivity extends BaseActivity{

	private ViewPager myViewPager;

	private WordPagerAdapter myAdapter;
	
	private LayoutInflater mInflater;
	private ArrayList<View> mListViews;
	private View layout1 = null;
	private View layout2 = null;
	private static WordList wl;
	
	private int[] NewLists;
	private ArrayList<Integer> ReviewLists;
	private StoreProgress sp;
	private WordSchedule ws;
	private boolean isAdded = false;
	private int newNum = 0;
	private int ReviewNum = 0;
	
	private void initWords()
	{
		int resNum = R.raw.w01 + NewLists[newNum] - 1;
		InputStream in = getResources().openRawResource(resNum);
        wl = new WordList(NewLists[newNum]);
        wl.LoadListStream(in);
	}
	
	private void setwl2Review()
	{
		int resNum = R.raw.w01 + ReviewLists.get(ReviewNum) - 1;
		InputStream in = getResources().openRawResource(resNum);
        wl = new WordList(ReviewLists.get(ReviewNum));
        wl.LoadListStream(in);
	}
	
	private void initActionBar()
	{
		getActivityHelper().setupActionBar("List"+Integer.toString(wl.ListNum), 0);
        getActivityHelper().addActionButtonCompat(R.drawable.ic_title_search, R.string.description_search,
                new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.i("Test","TEST!!!!!!!");
					}
				}, false);
	}
	
	private void setFirst2Pages()
	{
		mListViews = new ArrayList<View>();
        mInflater = getLayoutInflater();
        layout1 = mInflater.inflate(R.layout.page_word, null);
        layout2 = mInflater.inflate(R.layout.page_word, null);
        mListViews.add(layout1);
        mListViews.add(layout2);
		
        myAdapter = new WordPagerAdapter(mListViews);
        
		myViewPager = (ViewPager) findViewById(R.id.viewpagerLayout);
		myViewPager.setAdapter(myAdapter);
		
        myViewPager.setCurrentItem(0);
        
        
        TextView text1 = (TextView)layout1.findViewById(R.id.wordText);
        TextView text2 = (TextView)layout1.findViewById(R.id.contentText);
        WordBlock wb = wl.getNextWordRecite();
        text1.setText( wb.Word );
        text2.setText( wb.Meaning );
        text1 = (TextView)layout2.findViewById(R.id.wordText);
        text2 = (TextView)layout2.findViewById(R.id.contentText);
        wb = wl.getNextWordRecite();
        text1.setText( wb.Word );
        text2.setText( wb.Meaning );
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_recite);
		
		sp = new StoreProgress(this);
		sp.setStartDate2Today();
		int day = TimeManip.getDaysPassed( sp.getStartDate(), TimeManip.GetTimeStructNow() );
		ws = new WordSchedule();
		NewLists = ws.getNewWord(day);
		ReviewLists = ws.getReviewWord(day);
		
		initWords();
		initActionBar();
		setFirst2Pages();
		
        myViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0){
				WordBlock wb = wl.getNextWordRecite();
				if( wb == null )
				{
					if(isAdded == false && newNum+1 < NewLists.length)
					{
						View AnotherLayout = mInflater.inflate(R.layout.next_list_warning, null);
						Button BtnNext = (Button)AnotherLayout.findViewById(R.id.btn_nextList);
						BtnNext.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								newNum++;
								initWords();
								setFirst2Pages();
								isAdded = false;
								getActivityHelper().setActionBarTitle
									("List"+Integer.toString(wl.ListNum));
							}
						});
						mListViews.add(AnotherLayout);
						isAdded = true;
					}
					
					else if(isAdded == false && ReviewNum < ReviewLists.size())
					{
						View AnotherLayout = mInflater.inflate(R.layout.next_list_warning, null);
						Button BtnNext = (Button)AnotherLayout.findViewById(R.id.btn_nextList);
						BtnNext.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								setwl2Review();
								ReviewNum++;
								mListViews.clear();
								setFirst2Pages();
								isAdded = false;
								getActivityHelper().setActionBarTitle
									("List"+Integer.toString(wl.ListNum) + "(Review)");
							}
						});
						mListViews.add(AnotherLayout);
						isAdded = true;
					}
					else if(isAdded == false)
					{
						View Another= mInflater.inflate(R.layout.next_list_warning, null);
						TextView tx = (TextView)Another.findViewById(R.id.wordText);
						tx.setText("恭喜你已经把本次的任务完成了！！别忘了12个小时过后来复习哦！");
						Button bt = (Button)Another.findViewById(R.id.btn_nextList);
						bt.setVisibility(View.INVISIBLE);
						mListViews.add(Another);
						isAdded = true;
					}
				}
				else if( (arg0+1) == mListViews.size())
				{
					View AnotherLayout = mInflater.inflate(R.layout.page_word, null);
					mListViews.add(AnotherLayout);
					View v = mListViews.get(arg0+1);
					TextView t1 = (TextView)v.findViewById(R.id.wordText);
	 				TextView t2 = (TextView)v.findViewById(R.id.contentText);
					
					t1.setText(wb.Word);
					t2.setText(wb.Meaning);
				}
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}
		});
        
	}
        
}
